package com.teamblueplus.comicmaker;

import java.io.BufferedReader;

import java.io.BufferedInputStream;
import java.io.File;

import java.util.ArrayList;

import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.teamblueplus.R;
import com.teamblueplus.websearch.GridViewImageAdapter;

/**
 * Class contains objects for image edit
 * 
 * @author qui
 * 
 */
public class AddDefaultObject extends Activity implements OnItemClickListener {

	// private ListView listViewImages;
	private GridView gridViewImages;
	private EditText txtSearchText;

	private DefaultObjectGridViewAdapter adapter;
	private ArrayList<String> listImages;
	private Activity activity;

	String strSearch = null;
	ImageView image_view;
	Bitmap bitmap;

	private String message = "";
	private Handler mProgressHandler;
	// private GoogleImageBean bean;

	private String imagePath;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gridview_default);

		activity = this;

		// listViewImages = (ListView) findViewById(R.id.lviewImages);
		gridViewImages = (GridView) findViewById(R.id.gridViewDefault);

		txtSearchText = (EditText) findViewById(R.id.txtViewSearch);
		image_view = (ImageView) findViewById(R.id.imageview);

		listImages = new ArrayList<String>();
		new getImagesTask().execute();

	}

	public class getImagesTask extends AsyncTask<Void, Void, Void> {
		JSONObject json, json2;
		ProgressDialog dialog;

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();

			dialog = ProgressDialog.show(AddDefaultObject.this, "",
					"Please wait...");
		}

		@Override
		protected Void doInBackground(Void... params) {

			File sdCard = Environment.getExternalStorageDirectory();
			File dir = new File(sdCard.getAbsolutePath() + "/dimages");

			String files;
			File[] listOfFiles = dir.listFiles();

			Log.d("h2", "lengthOFDEfault::" + listOfFiles.length);
			int counter = 0;
			for (int i = 0; i < listOfFiles.length; i++) {
				if (listOfFiles[i].isFile()) {
					counter++;
					files = listOfFiles[i].getAbsolutePath();
					listImages.add(files);
					// System.out.println(files);
				}
			}

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			if (dialog.isShowing()) {
				dialog.dismiss();
			}
			setListViewAdapter(listImages);
		}
	}

	public void setListViewAdapter(ArrayList<String> images) {
		// adapter = new ListViewImageAdapter(activity, images);
		// listViewImages.setAdapter(adapter);
		// listViewImages.setOnItemClickListener(this);

		adapter = new DefaultObjectGridViewAdapter(activity, images);
		gridViewImages.setAdapter(adapter);
		gridViewImages.setOnItemClickListener(this);

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
		Intent intent = new Intent();

		Log.d("defaultObject", listImages.get(pos));

		intent.putExtra("path", listImages.get(pos));

		setResult(RESULT_OK, intent);
		finish();

	}

}